package com.ruoyi.system.service.impl;

import java.util.Date;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.SysUserDevelop;
import com.ruoyi.system.service.IDeptFileArchivedService;
import com.ruoyi.system.service.ISysUserDevelopService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysUserStage1Mapper;
import com.ruoyi.system.domain.SysUserStage1;
import com.ruoyi.system.service.ISysUserStage1Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 入党申请人阶段Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-15
 */
@Service
public class SysUserStage1ServiceImpl implements ISysUserStage1Service
{
    @Autowired
    private SysUserStage1Mapper sysUserStage1Mapper;
    @Autowired
    private ISysUserDevelopService sysUserDevelopService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 新增入党申请人阶段
     *
     * @param sysUserStage1 入党申请人阶段
     * @return 结果
     */
    @Override
    public int insertSysUserStage1(SysUserStage1 sysUserStage1)
    {
        sysUserStage1.setCreateTime(DateUtils.getNowDate());
        return sysUserStage1Mapper.insertSysUserStage1(sysUserStage1);
    }

    /**
     * 修改入党申请人阶段
     *
     * @param sysUserStage1 入党申请人阶段
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSysUserStage1(SysUserStage1 sysUserStage1)
    {
        String createBy = sysUserStage1.getCreateBy();
        String userName = sysUserStage1.getDevelopUserName();
        SysUserStage1 sysUserStage = sysUserStage1Mapper.selectSysUserStage1ByDevelopId(sysUserStage1.getSysUserDevelopId());
        sysUserStage1.setUpdateTime(DateUtils.getNowDate());
        Date step1ApplyTime = sysUserStage1.getStep1ApplyTime();
        Date step2TalkTime = sysUserStage1.getStep2TalkTime();
        SysUserDevelop sysUserDevelop = new SysUserDevelop();
        sysUserDevelop.setId(sysUserStage.getSysUserDevelopId());
        //父归档文件
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(sysUserStage1.getSysUserDevelopId()));
        if(!ObjectUtils.isEmpty(step1ApplyTime)){
            sysUserStage1.setStep1FinishTime(DateUtils.getNowDate());
            sysUserStage1.setStep1Status("1");
            sysUserDevelop.setDevelopStage("提交入党申请书阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);
            //创建阶段1归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("提交入党申请书阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage1.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段1文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("入党申请书");
            deptFileArchivedVo2.setFileUrl(sysUserStage1.getStep1File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage1.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);

        }else if(!ObjectUtils.isEmpty(step2TalkTime)){
            sysUserStage1.setStep2FinishTime(DateUtils.getNowDate());
            sysUserStage1.setStep2Status("1");
            sysUserDevelop.setDevelopStage("党组织派人谈话阶段");
            sysUserDevelopService.updateSysUserDevelop(sysUserDevelop);
            //创建阶段2归档文件夹
            DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
            deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo.setCreateBy(createBy);
            deptFileArchivedVo.setCreateName(userName);
            deptFileArchivedVo.setFileType("0");
            deptFileArchivedVo.setFileName("党组织派人谈话阶段");
            deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
            deptFileArchivedVo.setAssociationId(String.valueOf(sysUserStage1.getId()));
            Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
            //创建阶段2文件
            DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
            deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
            deptFileArchivedVo2.setCreateBy(createBy);
            deptFileArchivedVo2.setCreateName(userName);
            deptFileArchivedVo2.setFileType("1");
            deptFileArchivedVo2.setFileName("党支部派人谈话记录表");
            deptFileArchivedVo2.setFileUrl(sysUserStage1.getStep2File());
            deptFileArchivedVo2.setParentId(archivedId);
            deptFileArchivedVo2.setAssociationId(String.valueOf(sysUserStage1.getId()));
            deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        }
        sysUserStage1.setId(sysUserStage.getId());
        return sysUserStage1Mapper.updateSysUserStage1(sysUserStage1);
    }


    /**
     * 删除入党申请人阶段信息
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 结果
     */
    @Override
    public int deleteSysUserStage1ByUserDevelopId(Long userDevelopId){
        return sysUserStage1Mapper.deleteSysUserStage1ByUserDevelopId(userDevelopId);
    }

    /**
     * 查询入党申请人阶段
     *
     * @param userDevelopId 入党申请人阶段主键
     * @return 入党申请人阶段
     */
    @Override
    public SysUserStage1 selectSysUserStage1ByDevelopId(Long userDevelopId){
        return sysUserStage1Mapper.selectSysUserStage1ByDevelopId(userDevelopId);
    }
}
